package com.test.shardingjdbc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.shardingjdbc.entity.OrderEntity;
import com.test.shardingjdbc.entity.StockEntity;
import com.test.shardingjdbc.mapper.OrderEntityMapper;
import com.test.shardingjdbc.mapper.StockEntityMapper;
import com.test.shardingjdbc.service.OrderService;
import io.shardingsphere.transaction.annotation.ShardingTransactionType;
import io.shardingsphere.transaction.api.TransactionType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author sunchuanyin
 * @version 1.0
 * @description
 * @date 2022/5/4 3:01 下午
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderEntityMapper,OrderEntity> implements OrderService {

    @Resource
    private OrderEntityMapper orderEntityMapper;

    @Resource
    private StockEntityMapper stockEntityMapper;

    /**
     * 新增订单
     */
//    @Transactional(rollbackFor = Exception.class)
    // 分库事务（跨节点）
    @ShardingTransactionType(TransactionType.XA)
    @Override
    public void addOrder() {
        // ds2_202205
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setProductId(1L);
        orderEntity.setProductName("test2");
        orderEntity.setOrderStatus(1);
        orderEntity.setAmount(new BigDecimal(1020.12));
        orderEntity.setCreateTime(new Date());
        orderEntity.setTime("202205");
        orderEntityMapper.insert(orderEntity);


        int i = 1 / 0;
        // ds1_tb_stock
        StockEntity stockEntity = new StockEntity();
        stockEntity.setProductId(1L);
        stockEntity.setProductName("test2");
        stockEntity.setStock(10);
        stockEntity.setCreateTime(new Date());
        stockEntity.setUpdateTime(new Date());

        stockEntityMapper.insert(stockEntity);

    }
}
